exports.controllers={
	
	forum: function(space,options) {
		if (!options) options = {};

		var mode = space.fields.mode;
		if (!mode) {
			if (space.fields.category_id) mode="subcategory_view";
			if (space.fields.root_message_id) mode="messages_view";
			if (!mode) mode="root_categories_view";
		}

		return space.controllers["forum__"+mode](space,options);
	},
	
	forum__root_categories_view: function(space,options){
		options.template = "forums/root_categories";
		
		var root_categories =[];
		function get_level(parent_id,level){
			var i = 0;
			var categories = space.mfact.list("forum_category",{parent_id: parent_id,order_by: "main.ordering"});
			if (level<3) {
				for( i = 0 ; i < categories.length ; i++ ){
					categories[i].subcategories = get_level(categories[i].id,level+1);	
				}
			}
			return categories;
		};		
		space.root_categories = get_level(null,1);
			
		return space.views.process("widgets/"+options.template,space);
		//return space.Dumper(space.root_categories[0].subcategories);
	},
	
	
	forum__new_topic: function(space,options){
		options.template = "forums/new_topic";
		space.category=space.mfact.get("forum_category",space.fields.category_id);
		return space.views.process("widgets/"+options.template,space);
	},

	forum__subcategory_view: function(space,options){
		options.template = "forums/subcategory";
		space.category=space.mfact.get("forum_category",space.fields.category_id);
		var parent_id = space.fields.category_id;
		var subcategories = space.mfact.list("forum_category",{parent_id: parent_id,order_by: "main.ordering"});
		
		function get_subcategories(parent_id){
			var i = 0;
			var categories = space.mfact.list("forum_category",{parent_id: parent_id,order_by: "main.ordering"});
			if (categories) {
				for(i = 0 ; i < categories.length ; i++){
					categories[i].subcategories = get_subcategories(categories[i].id);	
				}
			}
			return categories;
		};
		space.subcategories = get_subcategories(parent_id);
		space.root_messages=space.mfact.list("forum_message",{parent_id:null,category_id:parent_id,order_by:"main.id desc"});

		return space.views.process("widgets/"+options.template,space);
	},
	
	
	forum__message_write: function(space) {
		var pmsg_id = space.fields.pmsg_id;
		var category_id = space.fields.category_id;
		var msg = space.mfact.from_object("forum_message",{
			body: space.fields.body,
			title: space.fields.title,
			category_id: category_id,
			user_cr_id: space.uid,
			deleted: 0,
			sticked: 0			
		});
		if(space.fields.parent_id){msg.parent_id = space.fields.parent_id}
		else{msg.parent_id = null};
		
		msg.Save();
		var root_message_id;
		if (space.fields.root_message_id) {
			
			root_message_id = space.fields.root_message_id;
		} else {
			root_message_id = msg.id;
		};
			
		return JSON.stringify({redirect: {url: space.action+category_id+"/"+root_message_id+"#"+msg.id}});
	},
	
	
	forum__messages_view: function(space){
		var category_id = space.fields.category_id;
		var root_message_id = space.fields.root_message_id;
		//var messages = space.mfact.list_hier("forum_message",{category_id: category_id, root_message_id : root_message_id , order_by: "main.id desc"});
		var messages = space.mfact.list_hier("forum_message",{category_id: category_id}, root_message_id);
		
		space.messages = messages;
		space.category=space.mfact.get("forum_category",space.fields.category_id);
			
		return space.views.process("widgets/forums/messages",space);	
	},
	
	
	
	forum__subcategory_write: function (space) {
		
		var parent_id = space.fields.parent_id;
		var category_id = space.fields.category_id;
		var category;
		if (category_id) {
			category = space.mfact.get("forum_category", {parent_id: parent_id, id: category_id, order_by: "main.id desc"}); 
		} else {
			category={};
		}
		space.category = category;

		space.roles=space.site.sql.execute_and_fetch("roles/list");
		space.roles.unshift({id:"",name:""});
		//throw new Error("message="+message.question);
		return space.views.process("widgets/forums/subcategory",space);
	},
	
	forum__message_delete: function(space) {
		var root_message_id = space.fields.root_message_id;
		var category_id = space.fields.category_id;
		var message_id = space.fields.msg_id;
		var can_delete = space.mfact.get("forum_message", {id: message_id});
		//throw space.Dumper(can_delete);	
			
		if(space.uid && can_delete){
			var item = space.mfact.get("forum_message",{id: message_id});	
			item.Remove();		
			return JSON.stringify({redirect: {url: space.action+category_id+"/"+root_message_id}});	
		} else {
			return JSON.stringify({call:"not_delete"});
		};
		
		
	},
	
	forum__sticked_message: function(space) {
		
		var root_message_id = space.fields.root_message_id;
		var category_id = space.fields.category_id;
		var item=space.mfact.get("forum_message",{id: space.fields.massege_id});
		var sticked = null;
		(space.fields.sticked) 
			? sticked = 1
			: sticked = 0;
		item.UpdateFromObject({sticked: sticked}); 
		item.Save();
		return JSON.stringify({redirect: {url: space.action+category_id+"/"+root_message_id}});	
	},
	

	
};

